package com.hp.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hp.bean.Master;
import com.hp.bean.MasterExample;
import com.hp.service.MasterService;
import com.hp.util.JsonData;
import com.hp.util.LngAndLatUtil;
import com.hp.util.RedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
@RequestMapping("/master")
public class MasterController {
    @Resource
    private MasterService service;
@Resource
RedisUtil redis;
    @RequestMapping("/find")
    @ResponseBody
    public Object find(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        MasterExample example = new MasterExample();
        MasterExample.Criteria criteria = example.createCriteria();


        PageInfo<Master> list =service.pageFind(example, page, limit);
        return JsonData.buildSuccess(list);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        Master master = service.findByid(id);
        return JsonData.buildSuccess(master);
    }
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Master master) {
        int i = service.add(master);
        return i==1? JsonData.buildSuccess("添加成功！"): JsonData.buildError("添加失败");
    }
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Master master) {
        int i = service.upp(master);
        return i==1? JsonData.buildSuccess("修改成功！"): JsonData.buildError("修改失败");

    }
    /**
     * 查询指定信息
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(Master obj) {
        if(!redis.hasKey("masterList")){
            System.out.println("mysql查询getList");
            obj.setStatus("1");
            List<Master> list = service.selectMasterList(obj);
            for (Master master : list) {
                if(!redis.hasKey(master.getAccount())){
                    try {
                        master.setAddress(LngAndLatUtil.getLngAndLat(master.getLng(), master.getLat()));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                    redis.set(master.getAccount(), master.getAddress(), 60);
                }else{
                    master.setAddress((String) redis.get(master.getAccount()));
                }
            }
            redis.set("masterList", JSONObject.toJSONString(list), 30*60);
        }
        return redis.get("masterList");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id) {
        int i = service.del(id);
        return i==1? JsonData.buildSuccess("停用成功！！"): JsonData.buildError("停用失败！");

    }
}
